﻿using System;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Impl;
using NHibernate.Tool.hbm2ddl;
using BankCreditPortfolio.DAL.Entities;

namespace BankCreditPortfolio.DAL
{
    public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                    InitializeSessionFactory();

                return _sessionFactory;
            }
        }

        private static void InitializeSessionFactory()
        {
            try
            {
                var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DreamTeamConnectionString"].ConnectionString;

                _sessionFactory = Fluently.Configure()
                    .Database(MsSqlConfiguration.MsSql2008
                                  .ConnectionString(connectionString)
                                  .ShowSql()
                    )
                    .Mappings(m =>
                    {
                        m.FluentMappings.AddFromAssemblyOf<Role>();
                        m.FluentMappings.AddFromAssemblyOf<Account>();
                        m.FluentMappings.AddFromAssemblyOf<AgreementPersistence>();
                        m.FluentMappings.AddFromAssemblyOf<Banker>();
                        m.FluentMappings.AddFromAssemblyOf<Client>();
                        m.FluentMappings.AddFromAssemblyOf<ContentPage>();
                        m.FluentMappings.AddFromAssemblyOf<Credit>();
                        m.FluentMappings.AddFromAssemblyOf<Currency>();
                        m.FluentMappings.AddFromAssemblyOf<Deposit>();
                        m.FluentMappings.AddFromAssemblyOf<DepositType>();
                        m.FluentMappings.AddFromAssemblyOf<DocumentType>();
                        m.FluentMappings.AddFromAssemblyOf<Guarantor>();
                        m.FluentMappings.AddFromAssemblyOf<OrderInfoPersistence>();
                        m.FluentMappings.AddFromAssemblyOf<OrderStatus>();
                        m.FluentMappings.AddFromAssemblyOf<PageTemplate>();
                        m.FluentMappings.AddFromAssemblyOf<Payment>();
                        m.FluentMappings.AddFromAssemblyOf<Payments>();
                        m.FluentMappings.AddFromAssemblyOf<PaymentStatus>();
                        m.FluentMappings.AddFromAssemblyOf<Profile>();
                        m.FluentMappings.AddFromAssemblyOf<TariffPersistence>();
                        m.FluentMappings.AddFromAssemblyOf<TariffsDocuments>();
                        //m.FluentMappings.AddFromAssemblyOf<OrderHistoryPersistence>();
                    })
                    //.ExposeConfiguration(cfg => new SchemaExport(cfg)
                    //                                .Create(false, true))
                    .BuildSessionFactory();
            }
            catch (Exception ex)
            { }
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}